import errorBag from './errorBag.js'
export default {
  required: (payload, vm) => {
    if (vm[payload.query] === 0) {
      return true
    } else {
      let t = vm[payload.query]
      if (Object.prototype.toString.call(t) === '[object String]') {
        t = t.replace(/^\s+|\s+$/g, '')
      }
      if (!t) {
        errorBag('required', null, payload)
        return false
      } else {
        return true
      }
    }
  },
  select: (payload, vm) => {
    if (!vm[payload.query]) {
      errorBag('select', null, payload)
      return false
    } else {
      return true
    }
  },
  ip: (payload, vm) => {
    let ip = vm[payload.query]
    let ipReg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
    if (!ipReg.test(ip)) {
      errorBag('ip', ip, payload)
      return false
    } else {
      return true
    }
  },
  date: (payload, vm) => {
    let date = vm[payload.query]
    let dateReg = /^\d{4}[-]\d{2}[-]\d{2}$/
    if (!dateReg.test(date)) {
      errorBag('date', date, payload)
      return false
    } else {
      return true
    }
  },
  min: (payload, vm) => {
    let length = vm[payload.query].length
    if (length < payload.min) {
      errorBag('min', payload.min, payload)
      return false
    } else {
      return true
    }
  },
  max: (payload, vm) => {
    let length = vm[payload.query].length
    if (length > payload.max) {
      errorBag('max', payload.max, payload)
      return false
    } else {
      return true
    }
  },
  minNum: (payload, vm) => {
    let number = Number.parseInt(vm[payload.query])
    if (number < payload.minNum) {
      errorBag('minNum', payload.minNum, payload)
      return false
    } else {
      return true
    }
  },
  maxNum: (payload, vm) => {
    let number = Number.parseInt(vm[payload.query])
    if (number > payload.maxNum) {
      errorBag('maxNum', payload.maxNum, payload)
      return false
    } else {
      return true
    }
  },
  number: (payload, vm) => {
    let numReg = new RegExp('^[0-9]*$')
    let num = vm[payload.query]
    if (!numReg.test(num)) {
      errorBag('number', null, payload)
      return false
    } else {
      return true
    }
  },
  numberDot: (payload, vm) => {
    let numReg = new RegExp('([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])')
    let num = vm[payload.query]
    if (!numReg.test(num)) {
      errorBag('number', null, payload)
      return false
    } else {
      return true
    }
  },
  phone: (payload, vm) => {
    let phoneReg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
    let phone = vm[payload.query]
    if (!phoneReg.test(phone)) {
      errorBag('phone', null, payload)
      return false
    } else {
      return true
    }
  },
  // 支持'-'和数字
  tel: (payload, vm) => {
    let telReg = /^([0-9]+-)?[0-9]+(-[0-9]+)?$/
    let tel = vm[payload.query]
    if (!telReg.test(tel)) {
      errorBag('tel', null, payload)
      return false
    } else {
      return true
    }
  },
  email: (payload, vm) => {
    let emailReg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/
    let email = vm[payload.query]
    if (!emailReg.test(email)) {
      errorBag('email', null, payload)
      return false
    } else {
      return true
    }
  },
  password: (payload, vm) => {
    let passwordReg = /^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$/
    let password = vm[payload.query]
    if (!passwordReg.test(password)) {
      errorBag('password', null, payload)
      return false
    } else {
      return true
    }
  },
  noPhone: (payload, vm) => {
    let phoneReg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
    let phone = vm[payload.query]
    if (phoneReg.test(phone)) {
      errorBag('noPhone', null, payload)
      return false
    } else {
      return true
    }
  },
  special: (payload, vm) => {
    let text = vm[payload.query]
    let regEn = /[`~!@#$%^&*()_+<>?:"{},./;'[\]]/im
    let regCn = /[·！#￥（——）：；“”‘、，|《。》？、【】[\]]/im
    if (regEn.test(text) || regCn.test(text)) {
      errorBag('special', null, payload)
      return false
    } else {
      return true
    }
  },
  money: (target) => {
    let value = target.value
    let amount = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
    if (value.endsWith('.')) {
      let length = value.length
      let pointLength = value.indexOf('.')
      if (length - 1 !== pointLength) {
        if (!amount.test(value)) {
          target.value = ''
          errorBag('money', null, target)
        }
      }
    } else {
      if (!amount.test(value)) {
        target.value = ''
        errorBag('money', null, target)
      }
    }
  },
  moneywithminus: (target) => {
    let value = target.value
    let amount = /(^-?[1-9](\d+)?(\.\d{1,2})?$)|(^-?0$)|(^-?\d\.\d{1,2}$)/
    if (value.endsWith('.')) {
      let length = value.length
      let pointLength = value.indexOf('.')
      if (length - 1 !== pointLength) {
        if (!amount.test(value)) {
          target.value = ''
          errorBag('moneywithminus', null, target)
        }
      }
    } else if (value.startsWith('-')) {
      let minusStartIndex = value.indexOf('-')
      let minusEndIndex = value.lastIndexOf('-')
      if (minusStartIndex !== minusEndIndex) {
        target.value = ''
        errorBag('moneywithminus', null, target)
      }
    } else {
      if (!amount.test(value)) {
        target.value = ''
        errorBag('moneywithminus', null, target)
      }
    }
  },
  day: (target) => {
    let value = target.value
    let amount = /^(\d+\.\d{1,1}|\d+)$/
    if (value.endsWith('.')) {
      let length = value.length
      let pointLength = value.indexOf('.')
      if (length - 1 !== pointLength) {
        if (!amount.test(value)) {
          target.value = ''
          errorBag('day', null, target)
        }
      }
    } else {
      if (!amount.test(value)) {
        target.value = ''
        errorBag('day', null, target)
      }
    }
  },
  retrieve: (payload, vm) => {
    if (vm.retrieveMids[payload.query] && vm.retrieveMids[payload.query].length > 0) {
      return true
    } else {
      errorBag('required', null, payload)
      return false
    }
  },
  area: (payload, vm) => {
    if (payload.option.every((item) => vm[item])) {
      return true
    } else {
      errorBag('area', null, payload)
      return false
    }
  }
}
